/*	Author		: Arga
 *	Created On	: Apr 25, 2011
 *	Description	: 
 */

package team8.paacdroid.activity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import team8.paacdroid.R;
import team8.paacdroid.adapter.ScheduleAdapter;
import team8.paacdroid.bean.Schedule;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ViewScheduleActivity extends ListActivity {
	// UI elements declarations
	private TextView tvRouteNo, tvRouteName, tvServiceTypeAndDirection;
	private ListView lvStops;
	
	// Var declarations
	private ScheduleAdapter sAdapter;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		// Load the layout
		setContentView(R.layout.view_schedule_activity);
		tvRouteNo = (TextView) findViewById(R.id.tvRouteNo);
		tvRouteName = (TextView) findViewById(R.id.tvRouteName);
		tvServiceTypeAndDirection = (TextView) findViewById(R.id.tvServiceTypeAndDirection);
		
		// Load the schedule data
		@SuppressWarnings("unchecked")
		Schedule schedule = (Schedule) getIntent().getSerializableExtra("Schedule");
		
		if (schedule != null) {
			// Get the route no and display it.
			tvRouteNo.setText(schedule.getRouteNo() != null ? schedule.getRouteNo() : "");
			
			// Get the route name and display it
			tvRouteName.setText(schedule.getRouteName() != null ? schedule.getRouteName() : "");
			
			// Get the service type and direction
			tvServiceTypeAndDirection.setText(schedule.getServiceType() != null ? schedule.getServiceType() : "");
			tvServiceTypeAndDirection.setText(schedule.getDirection() != null ?  tvServiceTypeAndDirection.getText() + " - " + schedule.getDirection() : tvServiceTypeAndDirection.getText());
			
			Date now = new Date();
			SimpleDateFormat sdfDate = new SimpleDateFormat("M/d/yyyy");
			SimpleDateFormat sdfDateTime = new SimpleDateFormat("M/d/yyyy h:mm aa");
			SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm aa");
			String nowDate = sdfDate.format(now);
			
			// Create a dummy arraylist of the items
			ArrayList<HashMap<String, String>> scheduleList = new ArrayList<HashMap<String, String>>();
			for (int i=0; i<schedule.getStopNames().size(); i++) {
				HashMap<String, String> foo = new HashMap<String, String>();
				foo.put("stopName", schedule.getStopNames().get(i));
				foo.put("areaName", schedule.getAreaNames().get(i));
				LinkedList<Date> link = new LinkedList<Date>();
				LinkedList<Date> tailLink = new LinkedList<Date>();
				LinkedList<Date> headLink = new LinkedList<Date>();
				for (String fak : schedule.getStopSchedules().get(schedule.getStopNames().get(i))) {
					try {
						link.add(sdfDateTime.parse(nowDate + " " + fak));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				// Try to put all times before current time into the end of linked list
				ListIterator<Date> itr = link.listIterator();
				while (itr.hasNext()) {
					Date baz = itr.next();
					if(now.after(baz)) {
						tailLink.add(baz);
					} else {
						headLink.add(baz);
					}
				}
				headLink.addAll(tailLink);
//				while (itr.hasPrevious()) {
//					itr.previous();
//				}
				String bar = "";
				int count = 0;
				ListIterator<Date> itr2 = headLink.listIterator();
				while (itr2.hasNext()) {
					count++;
					if (count == 1) {
						bar = "<strong>" + sdfTime.format(itr2.next()) + "</strong>";
					} else {
						bar = bar + " / " + sdfTime.format(itr2.next());
					}
				}
				foo.put("stopTimes", bar);
				scheduleList.add(foo);
			}
			
			// Attach the list adapter to display the data
			sAdapter = new ScheduleAdapter(this, R.layout.schedule_item, scheduleList);
			setListAdapter(sAdapter);
		}
	}
}
